<?php
    class feedParser
    {
        private $url;
        private $tabXml;
        private $tabFeed;
        private $tabItems;
        
        private $titleFeed;
        private $descriptionFeed;
        private $linkFeed;
        private $lastBuildDate;
        private $pubDateFeed;
        private $ttlFeed;
        
        public $listItems = array();
        
        private $isRss;
        
        public function __construct($string) {
            include_once("news.php");
            $this->url = $string;
            $this->tabXml = $this->getTabXml($this->url);
            $this->generateFeedInformation();
            $this->generateNewsList();
        }
        
        public function generateNewsList()
        {
            $i = 0;
            
            if ($this->isRss == true)
            {
                while ($this->tabXml->channel->item[$i] != null)
                {
                    $newItem = new News($this->titleFeed, $this->isRss);
                    $newItem->generateNews($this->tabXml->channel->item);
                    array_push($this->listItems, $newItem);
                    $i++;
                }
            }
            else
            {
                while ($this->tabXml->entry[$i] != null)
                {
                    $newItem = new News($this->titleFeed, $this->isRss);
                    $newItem->generateNews($this->tabXml->entry);
                    array_push($this->listItems, $newItem);
                    $i++;
                }                
            }
            //var_dump($this->listItems);
        }
        
        public function getTitleFeed()
        {
            return ($this->titleFeed);
        }
        
        public function getDescriptionFeed()
        {
            return ($this->descriptionFeed);
        }
        
        public function getLinkFeed()
        {
            return ($this->linkFeed);
        }
        
        public function getLastBuildDate()
        {
            return ($this->lastBuildDate);
        }
        
        public function getPubDateFeed()
        {
            return ($this->pubDateFeed);
        }
        
        public function getTtlFeed()
        {
            return ($this->ttlFeed);
        }
        
        public function affAll()
        {
            echo "Title : " . $this->titleFeed . "<br />";
            echo "Description : " . $this->descriptionFeed . "<br />";
            echo "Link : " . $this->linkFeed . "<br />";
            echo "lastBuild : " . $this->lastBuildDate . "<br />";
            echo "pubDate : " . $this->pubDateFeed . "<br />";
            echo "ttl : " . $this->ttlFeed . "<br />";
        }
        
        public function getTabXml($url)
        {
            return (simplexml_load_file($url, "SimpleXMLElement", LIBXML_NOCDATA));
        }
        
        public function generateFeedInformation()
        {
            if (isset($this->tabXml->attributes()->version) && $this->tabXml->attributes()->version == "2.0")
            {
                $this->isRss = true;
                $this->titleFeed = $this->tabXml->channel->title;
                $this->descriptionFeed = $this->tabXml->channel->description;
                $this->linkFeed = $this->tabXml->channel->link;
                $this->lastBuildDate = $this->tabXml->channel->lastBuildDate;
                $this->pubDateFeed = $this->tabXml->channel->pubDate;
                $this->ttlFeed = $this->tabXml->channel->ttl;
            }
            else
            {
                $this->isRss = false;
                $this->titleFeed = $this->tabXml->title;
                $this->descriptionFeed = $this->tabXml->subtitle;
                $this->linkFeed = $this->tabXml->link->attributes()->href;
                $this->lastBuildDate = $this->tabXml->updated;
                $this->pubDateFeed = $this->tabXml->updated;
                $this->ttlFeed = 0;
            }
        }        
    }
?>